<template>
  <div>
    <div class="search-panel">
      <el-form :inline="true" label-width="50">
        <el-form-item label="最新">
          <el-radio-group v-model="radioData" @change="changeRadio()">
            <el-radio :label="1">当天</el-radio>
            <el-radio :label="2">3天</el-radio>
            <el-radio :label="3">7天</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :default-time="['00:00:00','23:59:59']"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item v-if="type === 'hz'" label="过滤恒值">
          <el-checkbox
            v-model="query.change"
            :true-label="1"
            :false-label="0"
          />
        </el-form-item>
        <el-button type="primary" @click="initTable(null)">查询</el-button>
      </el-form>
    </div>
    <div style="display: flex;flex-direction: row;">
      <div class="table-panel" :style="{flex: type==='hz'?'0 0 50%':'1'}">
        <el-table
          ref="table"
          v-loading="tableLoading"
          class="grid-data-table"
          height="250px"
          style="width: 100%;"
          :data="tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="realTime" label="时间">
            <template slot-scope="scope">
              <div>{{ parseTime(scope.row.realTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
            </template>
          </el-table-column>
          <template v-if="type == 'hz'">
            <el-table-column prop="val" label="值" />
          </template>
          <template v-if="type == 'cb'">
            <el-table-column prop="minVal" label="最小值" />
            <el-table-column prop="maxVal" label="最大值" />
            <el-table-column prop="avgVal" label="平均值" />
            <el-table-column prop="emissionsVal" label="排放" />
          </template>
        </el-table>
      </div>
      <div v-if="type == 'hz'" class="chart-container" style="width: 50%;">
        <LineChart id="line-chart" :chart-data="tableData" height="260px" width="100%;" />
      </div>
    </div>
    <div class="pagination-container">
      <Pagination
        v-show="query.total>0"
        :total="query.total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="initTable(null)"
      />
    </div>

  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { getDict } from '@/utils/dict'
import { getDictLabel } from '@/utils'
import { getDatefmt } from '@/utils/common'
import { getRealTimeDz, getRealTimeCb } from '@/api/dataQuery/dataQuery'
import Pagination from '@/components/Pagination'
import LineChart from './LineChart'
export default {
  components: {
    Pagination, LineChart
  },
  props: {
    outletId: {
      type: Number,
      default: null
    },
    factorId: {
      type: Number,
      default: null
    },
    factorName: {
      type: String,
      default: null
    },
    type: {
      type: String,
      default: 'hz'
    },
    isChart: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      radioData: 1,
      time: [],
      tableLoading: false,
      query: {
        total: 0,
        page: 1,
        limit: 10,
        controlLevel: '',
        industryCategory: '',
        emissionType: '',
        alarmType: '',
        processingStatus: '',
        remove: '',
        process: '',
        duration: null,
        projectDepartmentId: null,
        enterpriseId: null,
        outletId: null,
        factorId: null,
        change: 0
      },
      resetState1: true,
      resetState2: true,
      tableData: [],
      controlLevels: [],
      industryCategorys: [],
      emissionTypes: [],
      alarmTypes: [],
      processingStatuss: [],
      removes: [],
      processs: []
    }
  },
  watch: {
  },
  created() {
    this.industryCategorys = getDict('e013')
    this.emissionTypes = getDict('e012')
    this.controlLevels = getDict('e014')
    this.alarmTypes = getDict('e001')
    this.processingStatuss = getDict('e003')
    this.removes = getDict('e002')
    this.processs = getDict('e004')
    this.changeRadio()
  },
  methods: {
    parseTime, getDict, getDictLabel,
    handleQueryYz(row) {
      this.$emit('queryOther', row)
    },
    initTable(queryTime = null) {
      if (queryTime) {
        this.time = queryTime
      }
      this.query.outletId = this.outletId
      this.query.factorId = this.factorId
      if (this.time !== '' && this.time !== null) {
        this.query.startDate = this.time[0]
        this.query.endDate = this.time[1]
      } else {
        this.query.startDate = null
        this.query.endDate = null
      }
      this.tableLoading = true
      let fn = getRealTimeDz
      if (this.type === 'cb') {
        fn = getRealTimeCb
      }
      fn(this.query).then(res => {
        const data = res.data.data.data
        console.log(data)
        this.tableData = data.records
        this.query.total = data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    changeRadio() {
      this.time = []
      const date = new Date()
      this.$set(this.time, 1, getDatefmt(date, 'yyyy-MM-dd') + ' 23:59:59')
      if (this.radioData === 1) {
        this.$set(this.time, 0, getDatefmt(new Date(), 'yyyy-MM-dd') + ' 00:00:00')
      } else if (this.radioData === 2) {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 3)), 'yyyy-MM-dd') + ' 00:00:00')
      } else {
        this.$set(this.time, 0, getDatefmt(new Date(new Date().setDate(date.getDate() - 7)), 'yyyy-MM-dd') + ' 00:00:00')
      }
    },
    handleSelectionChange(val) {
      let str = ''
      let state = true
      if (val.length > 0) {
        str = val[0].processingStatus
      }
      val.forEach(item => {
        if (item.processingStatus === str) {
          state = false
        } else {
          state = true
          return
        }
      })
      if (str === 'e003_01') {
        this.resetState1 = state
      } else if (str === 'e003_02') {
        this.resetState2 = state
      } else if (str === '') {
        this.resetState1 = state
        this.resetState2 = state
      }
    },
    handleDetails() {

    }
  }
}
</script>

<style lang="scss" scoped>

</style>
